package cc.vv.baselibrary.view.pulltorefresh;
/**
 * Created by Bys on 2017/8/7.
 */

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * 作者：Bys on 2017/8/7.
 * 邮箱：baiyinshi@vv.cc
 */

public class PullToRefreshRecyclerViewH extends PullToRefreshBase<RecyclerView> {
    public PullToRefreshRecyclerViewH(Context context) {
        super(context);
    }

    public PullToRefreshRecyclerViewH(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreshRecyclerViewH(Context context, Mode mode) {
        super(context, mode);
    }

    public PullToRefreshRecyclerViewH(Context context, Mode mode, AnimationStyle style) {
        super(context, mode, style);
    }

    @Override
    public final Orientation getPullToRefreshScrollDirection() {
        return Orientation.HORIZONTAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context,
                                                 AttributeSet attrs) {
        RecyclerView recyclerView = new RecyclerView(context, attrs);
        LinearLayoutManager ms = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
        recyclerView.setLayoutManager(ms);
        return recyclerView;
    }

    @Override
    protected void onPtrRestoreInstanceState(Bundle savedInstanceState) {
        try {
            super.onPtrRestoreInstanceState(savedInstanceState);
        }catch (Exception e) {}
    }

    @Override
    protected boolean isReadyForPullStart() {
        return isLastItemVisible();
    }

    @Override
    protected boolean isReadyForPullEnd() {

        return isFirstItemVisible();
    }





    /**
     * @return boolean:
     * @Description: 判断第一个条目是否完全可见
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isFirstItemVisible() {
        final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

        if (null == adapter || adapter.getItemCount() ==0) {
            if (DEBUG) {
//                Log.d(LOG_TAG, "isFirstItemVisible. Empty View.");
            }
            return true;
        } else {
            /**
             * This check should really just be:
             * mRefreshableView.getFirstVisiblePosition() == 0, but PtRListView
             * internally use a HeaderView which messes the positions up. For
             * now we'll just add one to account for it and rely on the inner
             * condition which checks getTop().
             */
            if (getFirstVisiblePosition() <= 1) {
                final View firstVisibleChild = mRefreshableView.getChildAt(0);
                if (firstVisibleChild != null) {
                    return firstVisibleChild.getLeft() >= mRefreshableView.getLeft();
                }
            }
        }

        return false;
    }

    /**
     * @return int: 位置
     * @Description: 获取第一个可见子View的位置下标
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private int getFirstVisiblePosition() {
        View firstVisibleChild = mRefreshableView.getChildAt(0);
        return firstVisibleChild != null ? mRefreshableView
                .getChildAdapterPosition(firstVisibleChild) : -1;
    }

    /**
     * @return boolean:
     * @Description: 判断最后一个条目是否完全可见
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isLastItemVisible() {
        final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

        if (null == adapter || adapter.getItemCount()==0) {
            if (DEBUG) {
//                Log.d(LOG_TAG, "isLastItemVisible. Empty View.");
            }
            return true;
        } else {
            final int lastItemPosition = adapter.getItemCount() - 1;
            final int lastVisiblePosition = getLastVisiblePosition();

            if (DEBUG) {
//                Log.d(LOG_TAG, "isLastItemVisible. Last Item Position: "
//                        + lastItemPosition
//                        + " Last Visible Pos: "
//                        + lastVisiblePosition);
            }

            /**
             * This check should really just be: lastVisiblePosition ==
             * lastItemPosition, but PtRListView internally uses a FooterView
             * which messes the positions up. For me we'll just subtract one to
             * account for it and rely on the inner condition which checks
             * getBottom().
             */
            if (lastVisiblePosition >= lastItemPosition - 1) {
                final int childIndex = lastVisiblePosition - getFirstVisiblePosition();
                final View lastVisibleChild = mRefreshableView.getChildAt(childIndex);
                if (lastVisibleChild != null) {
                    return lastVisibleChild.getRight() <= mRefreshableView.getRight();
                }
            }
        }

        return false;
    }

    /**
     * @return int: 位置
     * @Description: 获取最后一个可见子View的位置下标
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private int getLastVisiblePosition() {
        View lastVisibleChild = mRefreshableView.getChildAt(mRefreshableView
                .getChildCount() - 1);
        return lastVisibleChild != null ? mRefreshableView
                .getChildAdapterPosition(lastVisibleChild) : -1;
    }
}
